<template>
  <!-- 注销账号 -->
  <div>
    <el-dialog top="20vh" :title="$t('personInfo.xinxi53')" :visible.sync="dialogShow" width="561px"
      :close-on-click-modal="false">
      <div class="patientAdd_inner">
        <el-form ref="form" label-position="top" label-width="100px" :model="cancelForm" :size="formItemSize">
          <el-form-item :label="$t('add.add027')" prop="">
            <el-input v-model="cancelForm.password" style="width: 320px" :placeholder="$t('add.add028')" />
            <div class="tip">
              {{ $t('add.add029') }}
            </div>
          </el-form-item>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button plain :size="optionButtonSize" @click="cancleHandle">{{
          $t('personInfo.xinxi34')
        }}</el-button>
        <el-button type="primary" :size="optionButtonSize" @click="submitHandle">{{
          $t('add.add81')
        }}</el-button>
      </div>
    </el-dialog>

    <LogooutTip ref="LogooutTip" />
  </div>
</template>

<script>

import LogooutTip from './logoouttip.vue'; // 注销
import Cookies from 'js-cookie';
export default {
  name: 'BLZMasterIndex',
  components: {
    LogooutTip,
  },
  data() {
    return {
      dialogShow: false,
      cancelForm: {
        id: '',
        password: '',
      },
    };
  },

  mounted() { },

  methods: {
    cancleHandle() {
      this.cancelForm = {
        id: '',
        password: '',
      };
      this.dialogShow = false;
    },
    // 注销
    submitHandle() {
      if (!this.cancelForm.password) {
        return this.$message.error(this.$t('add.add028'));
      }
      this.$http
        .post('/sys/user/checkPassword', {
          password: this.cancelForm.password
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          } else {
            console.log('res----', res);
            if (!res.data) {
              return this.$message({
                message: this.$t('add.add083'),
                type: 'warning'
              });

            }
            this.$confirm(this.$t('add.add94'), this.$t('add.add140'), {
              confirmButtonText: this.$t('add.add81'),
              cancelButtonText: this.$t('add.add80'),
              type: 'warning',
            })
              .then(() => {
                this.cancelForm.id = localStorage.getItem('user_id');
                this.$http
                  .put('/sys/user/cancelAccount', this.cancelForm)
                  .then(({ data: res }) => {
                    if (res.code !== 0) {
                      return this.$message.error(res.msg);
                    } else {
                      this.$message.success(this.$t('add.add96'));
                      this.cancelForm = {
                        id: '',
                        password: '',
                      };
                      window.localStorage.removeItem('user_headUrl');
                      window.localStorage.removeItem('user_realName');
                      window.localStorage.removeItem('user_superAdmin');
                      window.localStorage.removeItem('user_organizedRoleId');
                      window.localStorage.removeItem('user_organizedType');
                      window.localStorage.removeItem('user_roleId');
                      window.localStorage.removeItem('user_jg_id');
                      window.localStorage.removeItem('user_owner');
                      window.localStorage.removeItem('user_permissionsList');
                      window.localStorage.removeItem('user_id');
                      window.localStorage.removeItem('user_name');
                      window.localStorage.removeItem('user_roleId');
                      window.localStorage.removeItem('user_organizedType');
                      window.localStorage.removeItem('user_adminRoleName');
                      window.localStorage.removeItem('tg_expand');
                      window.localStorage.removeItem('lang');
                      Cookies.remove("token");
                      this.$router.push('/login');
                    }
                  })
                  .catch((err) => {
                    console.log('err', err);
                  });
              })
              .catch(() => {
                this.$message({
                  type: 'info',
                  message: this.$t('add.add145'),
                });
              });

          }
        })
        .catch((err) => {
          console.log('err', err);
        });


      // 先不判断
      // setTimeout(() => {
      //     this.$refs.LogooutTip.dialogShow = true;
      // }, 500);
    },
  },
};
</script>

<style lang="scss" scoped>
.tip {
  font-size: 12px;
  font-weight: 400;
  color: #999;
}
</style>
